<?php
//根据传入拍摄团队获取图片和文字
//用于预览
//查询参数，有：
/*
 * shooting_team:按拍摄团队分类
 * shooting_people：按拍摄对象汇总
 * album_title：按图集标题汇总
 */
//返回示例
/*
{
	"title": [
		"BB",
		"CC",
		"DD",
		"日本小姐姐",
		"杂图"
	],
	"url": [
		"https://kyun.ltyuanfang.cn/tc/2020/07/12/e8f5faef6d65a.jpg",
		"https://kyun.ltyuanfang.cn/tc/2020/07/12/ce50968107b9a.jpg",
		"https://kyun.ltyuanfang.cn/tc/2020/07/12/09ff9ba6c0f9e.jpg",
		"https://kyun.ltyuanfang.cn/tc/2020/07/12/8fccdd9953f80.jpg",
		"https://kyun.ltyuanfang.cn/tc/2020/07/12/0a95837ed15b2.jpg"
	],
	"len": 5
}
*/
require "../config.php";
$operation = $_POST['operation']; //获取操作数

if ($operation == "album_title") {
    //按图集标题汇总
    $album_title = base64_decode($_POST['album_title']);
    $conn = new mysqli($MySqlHost, $MySqlUser, $MySqlPwd, $MySqlDatabaseName);
    if (!$conn) {
        die("连接失败: " . mysqli_connect_errno());
    } else {
        mysqli_query($conn, "set character set 'utf8'"); //读库
        mysqli_query($conn, "set names 'utf8'"); //写库
        $res = mysqli_query($conn, "SELECT img_url FROM img_all  WHERE album_title = '$album_title'");
        //获取所有图集第2张
        $num = mysqli_num_rows($res);
        for ($j = 0; $j < $num; $j++) {
            $r = mysqli_fetch_assoc($res);
            $img_and_msg_json['url_data'][$j] = $r['img_url'];
        }
        $img_and_msg_json['len'] = $num;

        echo json_encode($img_and_msg_json);
    }
} else if ($operation == "shooting_people") {
    $shooting_team = base64_decode($_POST['shooting_team']);
    $conn = new mysqli($MySqlHost, $MySqlUser, $MySqlPwd, $MySqlDatabaseName);
    if (!$conn) {
        die("连接失败: " . mysqli_connect_errno());
    } else {
        mysqli_query($conn, "set character set 'utf8'"); //读库
        mysqli_query($conn, "set names 'utf8'"); //写库
        $res = mysqli_query($conn, "SELECT album_title FROM img_all WHERE shooting_team = '$shooting_team' GROUP BY album_title");
        $img_and_msg_json = array();  //最终数组
        //获取所有图集标题
        $all_num = mysqli_num_rows($res);
        for ($i = 0; $i <= $all_num - 1; $i++) {
            $r = mysqli_fetch_row($res);
            $img_txt[$i] = $r[0];
            $img_and_msg_json['title_data'][$i] = $img_txt[$i];
        }
        //获取所有图集第2张
        for ($j = 0; $j < count($img_txt); $j++) {
            $res = mysqli_query($conn, "SELECT img_url FROM img_all WHERE album_title='$img_txt[$j]'");
            $r = mysqli_fetch_assoc($res);
            $img_and_msg_json['url_data'][$j] = $r['img_url'];
        }
        $img_and_msg_json['len'] = count($img_txt);
        echo json_encode($img_and_msg_json);
    }
    mysqli_free_result($res);
}
/*
$shooting_team = base64_decode($_POST['shooting_team']);
$conn = new mysqli($MySqlHost, $MySqlUser, $MySqlPwd, $MySqlDatabaseName);
if (!$conn) {
    die("连接失败: " . mysqli_connect_errno());
} else {
    mysqli_query($conn, "set character set 'utf8'"); //读库
    mysqli_query($conn, "set names 'utf8'"); //写库
    $res = mysqli_query($conn, "SELECT album_title FROM img_all WHERE shooting_team = '$shooting_team' GROUP BY album_title");
    $img_and_msg_json = array();  //最终数组
    //获取所有图集标题
    $all_num = mysqli_num_rows($res);
    for ($i = 0; $i <= $all_num - 1; $i++) {
        $r = mysqli_fetch_row($res);
        $img_txt[$i] = $r[0];
        $img_and_msg_json['title_data'][$i] = $img_txt[$i];
    }
    //获取所有图集第2张
    for ($j = 0; $j < count($img_txt); $j++) {
        $res = mysqli_query($conn, "SELECT img_url FROM img_all WHERE album_title='$img_txt[$j]'");
        $r = mysqli_fetch_assoc($res);
        $img_and_msg_json['url_data'][$j] = $r['img_url'];
    }
    $img_and_msg_json['len'] = count($img_txt);
    echo json_encode($img_and_msg_json);
}
mysqli_free_result($res);
*/
